import numpy as np
import re
'''
These are important utils functions
'''
#%%
class utils():

    def __init__(self):
        pass
  
    def clean_name(self, X):
        '''
        This should be a cleaner version of clean_name_SCARTI
        '''
        r = []
        X = list(X)
        for i in range(len(X)):
            a  = X[i].lower()
            a = a.replace('.com', '')
            a = re.sub(r"\([^()]*\)", "", a)
            #a = re.sub(r"[^\w\t ]", '', a)
            r.append(''.join(a.replace(' company ', '').replace(' corporation', '').\
                             replace(' incorporated', '').replace('a shares', '').\
                             replace(' corp', '').replace(' llc', '').\
                             replace(' ltd', '').replace(' inc', '').\
                             replace(' sa', '').replace(' lp', '').\
                             replace(' spa', '').\
                             replace(' sanv', '').replace(' nv', '').\
                             replace(' plc', '').replace(' nvsa', '').\
                             replace(' ptd', '').replace(' int', '').\
                             replace(' international', '').replace('int l', '').\
                             replace('limited', '').replace('group', '').\
                             replace(' ', '').replace('  ', '').replace('.', '').replace(',', ''))
                     )   
        return(r)     
    def make_mrg(self, X, date_type):
        try:
            mrg = X['gvkey'].astype(int).astype(str)+'-'+X[date_type].astype(int).astype(str)
        except Exception:
            mrg = X['gvkey'].astype(int).astype(str)+'-'+X[date_type].astype(str)
        return(mrg) 
    
    def stdErr(self, X): return(X.dropna().std()/np.sqrt(len(X.dropna())))
    def stdErrBootstrapped(self, X,N=5000, func='np.median'):
        n  =  int(len(X)*0.85)
        r = []
        for i in range(N):
            tmp = np.random.choice(np.ravel(X.values).tolist(), n, replace = True)
            r.append(eval(func)(tmp))
        stdError = np.std(r)
        return(stdError)

    def significance(self, X): return(['***' if X < 0.01 else '**' if X < 0.05 else '*' if X < 0.1 else ''][0])
    
    

#%% Usage examples
if __name__ == '__main__':
    a=0    
    
    
    
    